﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Windows.Forms;
using System.IO;
using System.Drawing;

namespace LIK.NetAssemblyChecker
{
    internal class ReferencedAssembly : AssemblyObject
    {

        AssemblyName _aName;

        internal ReferencedAssembly(AssemblyName aName, AssemblyObject parent):base( "", 3, parent)
        {
            _aName = aName;
        }

        internal TreeNode GetTreeNode()
        {
            string assemblyName = _aName.Name;
            string nodename = String.Format("{0} [{1}]", _aName.Name, _aName.Version);
            TreeNode tr = base.GetTreeNode(nodename, _aName);

            Assembly assembly = null;

            //if (!Commons.NetSystemAssemblyNames.Contains(assemblyName))
            //{
            string file = Parent.GetFolder();
            if(!file.EndsWith("\\"))
                file = file + "\\";
            string path = Path.Combine(file, assemblyName + ".dll");
            if (!File.Exists(path))
                path = Path.Combine(file, assemblyName + ".exe");

            assembly = Commons.TryToLoadAssemblyFromFile(path);
            if (assembly == null)
            {
                assembly = Commons.TryToLoadAssemblyFromName(assemblyName);
            }

            if (assembly == null)
            {
                //mark file doesn't exist
                tr.ForeColor = Color.Red;
            }
            tr.Tag = assembly;
            // }
            Commons.CheckAssemblyDependants(nodename);
            return tr;
        }


    }
}
